/*
    Created by IntelliJ IDEA.
    @Auther: gaozhe
    @Date: 2020/5/12 上午 8:30
    
*/
package gaozhe.utils;

import org.apache.commons.io.FileUtils;

import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.*;

public class DownloadUtil {

    static String fileFolderName = null;
    static File homeDirectory = FileSystemView.getFileSystemView().getHomeDirectory();





    public static void downloadImgByUrlHashMap(HashMap<String,String> hashMap){
        //创建文件夹
        fileFolderName = hashMap.get("fileFolderName");
        String path = homeDirectory.getPath();
        String pathAndName = path + fileFolderName;
        File newFolder = new File(pathAndName);
        if (!newFolder.exists()){
            newFolder.mkdir();
        }
        int i = 1;
        int j = 1;

        try {



            for (String key:hashMap.keySet()){
                //获取图片链接
                if (key != "fileFolderName"){
                    System.out.println("开始下载"+key+" : " +hashMap.get(key));

                    URL url = new URL(hashMap.get(key));

                    File destinationFile = new File(pathAndName + "\\" + (i++) + ".jpg");

                    //commons-IO包下载
                    FileUtils.copyURLToFile(url,destinationFile);

                    System.out.println("--第"+j+++"张图片已下载完成--");

                    }
                }




        } catch (Exception e) {
            e.printStackTrace();
        }

    }



}
